/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
 */

package com.ewerp.mud.commands;

import com.ewerp.mud.core.persistence.IPersistenceManager;
import com.ewerp.mud.core.persistence.IPersistenceSession;
import com.ewerp.mud.core.session.parsers.TerminalLineParser.ITerminalLinePacketParser;
import com.ewerp.mud.entities.IEntity;
import com.ewerp.mud.entities.IObjectId;
import com.ewerp.mud.entities.ObjectId;
import com.ewerp.mud.entities.admin.Admin;

/**
 * Allow an Admin to possess an object in the mud.
 * 
 * @author cboyden
 * 
 */
public class PossessCommand extends BaseCommand {
    public static class PossessCommandTerminalLinePacketParser implements ITerminalLinePacketParser {
        @Override
        public ICommand parseTerminalLinePacket(IEntity source, String input) {
            PossessCommand cmd = null;
            final String[] tokens = input.split(" ");

            if (tokens.length > 1) {

                if (source instanceof Admin) {
                    cmd = new PossessCommand();
                    cmd.setAdmin((Admin) source);
                    cmd.setTargetId(new ObjectId(tokens[1]));
                }
            }

            return cmd;
        }
    }

    private Admin admin;
    private IObjectId targetId;

    @Override
    public void execute() throws IllegalStateException {
        IEntity entityToPossess = null;
        IPersistenceSession persistence = ((IPersistenceManager)getPluginManager().getPlugin(IPersistenceManager.class)).openSession();
        entityToPossess = persistence.fetch(targetId);
        admin.setPossessedEntity(entityToPossess);
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setTargetId(IObjectId targetId) {
        this.targetId = targetId;
    }

    public IObjectId getTargetId() {
        return targetId;
    }

}
